﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hiflo
{
    public partial class DateTimeFilter : Form
    {
        public DateTimeFilter()
        {
            InitializeComponent();
        }

        //实例化委托
        public filter_dataTime filterDateTimeHandler;

        //定义时间过滤的起始
        public DateTime m_dateTime_start=new DateTime();
        public DateTime m_dateTime_end = new DateTime();


        private void DateTimeFilter_Load(object sender, EventArgs e)
        {

        }



        public void init_by(DateTime dt_start, DateTime dt_end,DateTime dt_min,DateTime dt_max)
        {
            m_dateTime_start = new DateTime(dt_start.Year, dt_start.Month, dt_start.Day, 0, 0, 0);
            m_dateTime_end = new DateTime(dt_end.Year, dt_end.Month, dt_end.Day, 23, 59, 59);


            DateTime dtMin= new DateTime(dt_min.Year, dt_min.Month, dt_min.Day, 0, 0, 0);
            DateTime dtMax = new DateTime(dt_max.Year, dt_max.Month, dt_max.Day, 23, 59, 59);

            //指定格式，只显示"年/月/日"
            this.dateTimePicker_start.Format = DateTimePickerFormat.Custom;
            this.dateTimePicker_start.CustomFormat = "yyyy/MM/dd";
            this.dateTimePicker_end.Format = DateTimePickerFormat.Custom;
            this.dateTimePicker_end.CustomFormat = "yyyy/MM/dd";

            //设定范围
            this.dateTimePicker_start.MinDate = dtMin;
            this.dateTimePicker_start.MaxDate = dtMax;
            this.dateTimePicker_end.MinDate = dtMin;
            this.dateTimePicker_end.MaxDate = dtMax;

            this.dateTimePicker_start.Text = m_dateTime_start.ToString();
            this.dateTimePicker_end.Text = m_dateTime_end.ToString();
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            //调用委托，处理
            filterDateTimeHandler(m_dateTime_start, m_dateTime_end);
            this.Close();
        }

        private void dateTimePicker_start_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt_start = this.dateTimePicker_start.Value;
            if (dt_start > this.dateTimePicker_end.Value)
            {
                this.dateTimePicker_start.Value = m_dateTime_start;
                MessageBox.Show(LanguageMngr.pls_re_choose_datetime());
            }
            else
            {
                m_dateTime_start = m_dateTime_start = new DateTime(dt_start.Year, dt_start.Month, dt_start.Day, 0, 0, 0);
            }
        }

        private void dateTimePicker_end_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt_end=this.dateTimePicker_end.Value;
            if (m_dateTime_start > this.dateTimePicker_end.Value)
            {
                this.dateTimePicker_end.Value = m_dateTime_end;
                MessageBox.Show(LanguageMngr.pls_re_choose_datetime());
            }
            else
            {
                m_dateTime_end = new DateTime(dt_end.Year, dt_end.Month, dt_end.Day, 23, 59, 59);
            }
        }
    }
}
